<template>
    <el-row class="home" :gutter="20">
        <el-col :span="8" style="margin-top:20px">
            <el-card shadow="hover">
                <div class="user">
                    <img :src="getImageUrl" alt="头像" class="user" />
                </div>
                <div class="user-info">
                    <p class="user-info-admin">Admin</p>
                    <p class="user-info-p">超级管理员</p>
                </div>
                <div class="login-info">
                    <p>上次登录时间:<span>{{ new Date().toLocaleString() }}</span></p>
                    <p>上次登录的地点:<span>北京</span></p>
                </div>
            </el-card>

            <el-card shadow="hover" class="table">
                <el-table :data="tableData">
                    <el-table-column v-for="(val, key) in tableLabel" :key="key" :prop="key" :label="val">
                    </el-table-column>
                </el-table>
            </el-card>
        </el-col>
        <el-col :span="16" style="margin-top:20px">
            <div class="num">
                <!-- 添加debug信息查看数据是否正确加载 -->
                <div v-if="countData.length === 0" style="width: 100%; text-align: center; color: #999; padding: 20px;">
                    暂无数据，请检查数据加载
                </div>
                <el-card :body-style="{ display: 'flex', padding: '0', height: '100%' }" v-for="item in countData"
                    :key="item.name">
                    <component :is="item.icon" class="icons" :style="{ background: item.color }"></component>
                    <div class="detail">
                        <p class="number">{{ item.value }}</p>
                        <p class="txt">{{ item.name }}</p>
                    </div>
                </el-card>
            </div>
            <el-card class="top-echart">
                <div ref="echart" style="height: 280px;"></div>
            </el-card>
            <div class="graph">
                <el-card>
                    <div ref="userChart" style="height: 240px;"></div>
                </el-card>
                <el-card>
                    <div ref="videoChart" style="height: 240px;"></div>
                </el-card>
            </div>
        </el-col>
    </el-row>
</template>

<script setup>
import { ref, reactive, onMounted, inject } from 'vue'
import userImage from '@/assets/images/user.png'
import * as echarts from 'echarts';
const getImageUrl = userImage
const api = inject('api', () => {
    console.log("api导入失败")
})
const tableData = ref([])
const countData = ref([])
const echart = ref(null)
const userChart = ref(null)
const videoChart = ref(null)
const observe = ref(null)

const tableLabel = ref({
    name: "课程",
    todayBuy: "今日购买",
    monthBuy: "本月购买",
    totalBuy: "总购买",
})
const getTableData = async () => {
    const data = await api.getTableData()
    // console.log(data)
    tableData.value = data
}
const getCountData = async () => {
    const data = await api.getCountData()
    // console.log(data)
    countData.value = data
}
//这个是折线图和柱状图 两个图表共用的公共配置
const xOptions = reactive({
    // 图例文字颜色
    textStyle: {
        color: "#333",
    },
    legend: {},
    grid: {
        left: "20%",
    },
    // 提示框
    tooltip: {
        trigger: "axis",
    },
    xAxis: {
        type: "category", // 类目轴
        data: [],
        axisLine: {
            lineStyle: {
                color: "#17b3a3",
            },
        },
        axisLabel: {
            interval: 0,
            color: "#333",
        },
    },
    yAxis: [
        {
            type: "value",
            axisLine: {
                lineStyle: {
                    color: "#17b3a3",
                },
            },
        },
    ],
    color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
    series: [],
})
const getChartsData = async () => {
    const { orderData, userData, videoData } = await api.getChartsData() // 正确访问数据结构
    // 第一个表格渲染，折线图
    xOptions.xAxis.data = orderData.date
    // 根据data数组生成series数据
    const firstItem = orderData.data[0]
    xOptions.series = Object.keys(firstItem).map(key => {
        return {
            name: key,
            data: orderData.data.map(item => item[key]),
            type: 'line'
        }
    })
    const oneChart = echarts.init(echart.value)
    //setOption方法应用配置对象
    oneChart.setOption(xOptions)

    // 第二个表格渲染，柱状图
    xOptions.xAxis.data = userData.map(item => item.date)
    //这样会使series变成一个二维数组，不符合图标规范
    // xOptions.series = userData.map(item => ([
    //     {
    //         name: '新增用户',
    //         data: item.new,
    //         type: 'bar'
    //     },
    //     {
    //         name: '活跃用户',
    //         data: item.active,
    //         type: 'bar'
    //     }
    // ]))
    xOptions.series = [
        {
            name:'新增用户',
            data:userData.map(item => item.new),
            type:'bar'
        },
        {
            name:'活跃用户',
            data:userData.map(item => item.active),
            type:'bar'
        }
    ]
    const twoChart = echarts.init(userChart.value)
    twoChart.setOption(xOptions)
    
    //第三个图表渲染，饼状图
    xOptions.series = {
        data:videoData,
        type:'pie'
    }
    const threeChart = echarts.init(videoChart.value)
    threeChart.setOption(xOptions)
    //监听窗口变化
    window.addEventListener('resize',() =>{
        oneChart.resize()
        twoChart.resize()
        threeChart.resize()
    })
}
onMounted(() => {
    getTableData()
    getCountData()
    getChartsData()
})


</script>

<style scoped lang="less">
.home {
    height: 100%;
    overflow: hidden;

    .user {
        display: flex;
        align-items: center;
        border-bottom: 1px solid #ccc;
        margin-bottom: 20px;

        img {
            width: 150px;
            height: 150px;
            border-radius: 50%;
            margin-right: 40px;

        }

        .user-info {
            p {
                line-height: 40px;
            }

            .user-info-p {
                color: #999;
            }

            .user-info-admin {
                font-size: 35px
            }
        }
    }

    .login-info {
        p {
            line-height: 30px;
            font-size: 14px;
            color: #999;

            span {
                color: #666;
                margin-left: 60px;
            }
        }
    }

    .user-table {
        margin-top: 20px;
    }

    .num {
        // 修复缩进，确保它与其他类同级
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;

        .el-card {
            width: 32%;
            margin-bottom: 20px;
            height: 80px; // 设置卡片高度与图标高度相同
        }

        .icons {
            width: 80px;
            height: 80px;
            font-size: 30px;
            text-align: center;
            line-height: 80px;
            color: #fff;
        }

        .detail {
            margin-left: 15px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            flex: 1; // 让detail盒子占据剩余空间
            min-width: 0; // 允许内容超出时收缩
            height: 100%; // 确保高度与卡片一致
            color: #333; // 确保文本颜色可见

            .number {
                font-size: 30px;
                margin-bottom: 10px;
                line-height: 1;
            }

            .txt {
                font-size: 15px;
                text-align: center;
                color: #666; // 调整文本颜色确保可见
                line-height: 1;
            }
        }
    }

    .graph {
        margin-top: 20px;
        display: flex;
        justify-content: space-between;

        .el-card {
            width: 48%;
            height: 320px;
        }
    }
}
</style>